<?php
/**
	Script used to get the data associate to an account or to edit an account
*/
require_once('header_redirection.php');
require_once('Class/connexionDB.Class.php');
require_once('Class/account.Class.php');

$account 	= new account();

if (isset($_POST['emailEditAccountSearch']))
{
	if ($_POST['emailEditAccountSearch'] != '' && filter_var($_POST['emailEditAccountSearch'], FILTER_VALIDATE_EMAIL))
	{
		$data = $account->getDataAssociateToThisEmail($_POST['emailEditAccountSearch']);
		$urlGET = "";
		
		if (isset($data['firstname']))
		{
			$urlGET .= 'firstname='. $data['firstname'] . '&';
		}
		if (isset($data['lastname']))
		{
			$urlGET .= 'lastname='. $data['lastname'] . '&';
		}
		if (isset($data['id_account_type']))
		{
			$urlGET .= 'id_account_type='. $data['id_account_type'] . '&';
		}
		if (isset($data['email']))
		{
			$urlGET .= 'email='. $data['email'] . '&';
		}
		if (isset($data['id_groups']))
		{
			$urlGET .= 'id_groups='. $data['id_groups'] . '&';
		}
		if (isset($data['id_country']))
		{
			$urlGET .= 'id_country='. $data['id_country'] . '&';
		}
		if (isset($data['team']))
		{
			$urlGET .= 'team='. $data['team'];
		}
		
		if ($urlGET == "")
		{
			header("Location: administration.php?noresult=". $_POST['emailEditAccountSearch']);	
		}
		else
		{
			header("Location: administration.php?" . $urlGET);
		}
		exit;
	}
	else
	{
		header("Location: administration.php?noresult=". $_POST['emailEditAccountSearch']);	
	}
}
else
{
	$groups = implode(';', $account->listedGroup($_POST)) . ';';

	if (empty($errorTab))
	{
		$newAccount 					= array();
		$newAccount['email'] 			= $_POST['emailEditAccount'];
		$newAccount['firstname'] 		= $_POST['firstnameEditAccount'];
		$newAccount['lastname'] 		= $_POST['lastnameEditAccount'];
		$newAccount['id_account_type'] 	= $_POST['accountEditType'];
		$newAccount['groups'] 			= $groups;
		$newAccount['country'] 			= $_POST['countryEditAccount'];
		$newAccount['team']				= $_POST['editTeam'];
		
		if (isset($_POST['passwordEditAccount']) && isset($_POST['passwordEditRepeat']) && $_POST['passwordEditAccount'] == $_POST['passwordEditRepeat'] && $_POST['passwordEditAccount'] != '')
		{
			$account->updatePassword($newAccount['email'], $_POST['passwordEditAccount']);
		}

		$account->update($newAccount);
	}

	$data = $account->getDataAssociateToThisEmail($newAccount['email']);
	$urlGET = "";
	
	if (isset($data['firstname']))
	{
		$urlGET .= 'firstname='. $data['firstname'] . '&';
	}
	if (isset($data['lastname']))
	{
		$urlGET .= 'lastname='. $data['lastname'] . '&';
	}
	if (isset($data['id_account_type']))
	{
		$urlGET .= 'id_account_type='. $data['id_account_type'] . '&';
	}
	if (isset($data['email']))
	{
		$urlGET .= 'email='. $data['email'] . '&';
	}
	if (isset($data['id_groups']))
	{
		$urlGET .= 'id_groups='. $data['id_groups'] . '&';
	}
	if (isset($data['id_country']))
	{
		$urlGET .= 'id_country='. $data['id_country'] . '&';
	}
	if (isset($data['team']))
	{
		$urlGET .= 'team='. $data['team'];
	}
	if ($urlGET == "")
	{
		header("Location: administration.php?noresult=". $_POST['emailEditAccount']);	
	}
	else
	{
		header("Location: administration.php?" . $urlGET . "&accountstatus=updated");	
	}
	
	exit;
}	
?>